package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import easier.recycler.BaseViewHolder;
import easier.recycler.MultiTypeAdapter;
import easier.recycler.TypeViewHolder;
import java.util.ArrayList;
import java.util.List;

public class MainActivity100 extends AppCompatActivity {

    private static final String TAG = "MainActivity100";

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity100.class );
        context.startActivity( starter );
    }

    private RecyclerView recycler;
    private MultiTypeAdapter mAdapter;
    private Button click0;
    private Button click1;

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main100 );
        initView();
    }

    private void initView() {

        recycler = (RecyclerView) findViewById( R.id.recycler );
        recycler.setLayoutManager( new LinearLayoutManager( this ) );
        mAdapter = new MultiTypeAdapter( convert() );
        recycler.setAdapter( mAdapter );
        click0 = (Button) findViewById( R.id.click0 );
        click1 = (Button) findViewById( R.id.click1 );

        click0.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View view ) {

                mAdapter.notifyItemChanged( 0 );
            }
        } );

        click1.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View view ) {

                mAdapter.notifyItemChanged( 0, "" );
            }
        } );
    }

    private List< TypeViewHolder > convert() {

        ArrayList< TypeViewHolder > list = new ArrayList<>();
        list.add( new ViewHolder() );
        return list;
    }

    private static class ViewHolder extends BaseViewHolder {

        public ViewHolder() {

            super( R.layout.activity_main_99_item );
        }

        @Override
        protected void onBind( RecyclerView.ViewHolder viewHolder, View root, int position, int type,
            @NonNull List< Object > payloads ) {

            super.onBind( viewHolder, root, position, type, payloads );
            ((TextView) root.findViewById( R.id.text )).setText( "Hello world payloads" );
            Log.i( TAG, "onBind: " + root + " " + payloads );
        }
    }
}